﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
using System.Data.SqlClient;

namespace DAO
{
    public class LoaiPhongDAO
    {
        public class SqlConnectionData
        {
            public static SqlConnection Connection()
            {
                SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=QLKS;Integrated Security=True");
                return conn;
            }
        }

        public static DataTable GetAll()
        {
            SqlConnection conn = SqlConnectionData.Connection();
            SqlCommand cmd = new SqlCommand("sp_LoaiPhongGetAll",conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
           
        }

        public static void InsertLoaiPhong(LoaiPhongDTO lp)
        {
            SqlConnection conn = SqlConnectionData.Connection();
            SqlCommand cmd = new SqlCommand("sp_InsertLoaiPhong", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@TenLoaiPhong", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@SoKhachToiDa", SqlDbType.Int);
            cmd.Parameters.Add("@DonGia", SqlDbType.Float);
            cmd.Parameters["@TenLoaiPhong"].Value = lp.TenLoaiPhong;
            cmd.Parameters["@SoKhachToiDa"].Value = lp.SoKhachToiDa;
            cmd.Parameters["@DonGia"].Value = lp.DonGia;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        public static void UpdatetLoaiPhong(LoaiPhongDTO lp)
        {
            SqlConnection conn = SqlConnectionData.Connection();
            SqlCommand cmd = new SqlCommand("sp_UpdateLoaiPhong", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@MaLoaiPhong",SqlDbType.Int);
            cmd.Parameters.Add("@TenLoaiPhong", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@SoKhachToiDa", SqlDbType.Int);
            cmd.Parameters.Add("@DonGia", SqlDbType.Float);
            cmd.Parameters["@TenLoaiPhong"].Value = lp.TenLoaiPhong;
            cmd.Parameters["@SoKhachToiDa"].Value = lp.SoKhachToiDa;
            cmd.Parameters["@DonGia"].Value = lp.DonGia;
            cmd.Parameters["@MaLoaiPhong"].Value = lp.MaLoaiPhong;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        public static DataTable LayLoaiPhong()
        {
            string sql = "Select TenLoaiPhong, MaLoaiPhong From LOAIPHONG";
            DataTable dt = DataProvider.ExecuteQuery(sql);
            return dt;
        }
        public static DataTable LoaiPhongTheoTen(string ten)
        {
            string sql = "Select * From LOAIPHONG Where TenLoaiPhong = '" + ten + "'";
            DataTable dt = DataProvider.ExecuteQuery(sql);
            return dt;
        }
    }
}
